is this a way of creating an array of 3 int called x in C++??
(I've seen this somewhere.)
is this a way of creating an array of 3 int called x in C++??
(I've seen this somewhere.)
yes it is
but what's the difference between
andCode:int x[3] = {0};
???Code:int x = new int[3];
> int x = new int[3];
This should be
Code:int *x = new int[3];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The former creates an array named x, of 3 ints, and zero initialises them. The latter will not even compile.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That syntax isn't quite right. It should be:
This is called dynamic allocation. The benefit is you can allocate whatever sized array you need at runtime. The disadvantage is you have to ensure the delete is called when you no longer need the array.Code:int* x = new int[3]; //... delete [] x;
In C++ an alternative to this could be std::vector
Edit: Wow I'm slow...
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
to laser light, why won't it compile??
Read the posts by Salem and JaWiB. You need a pointer to int.to laser light, why won't it compile??
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
okkkk!!! thanks!!
Sorry for the silly that silly post ;P